<?
if (!class_exists("go_sql")){
include("/var/www/html/new/conf/le.je");
  $le=new go_sql();
  $le->connect();
}

//外部接口
$ac='123697';
$pw='9abc595ba231a5e732f5c2e92e483287';
$outSide=new SoapClient("http://open.12301.cc/openService/MXSE.wsdl",array('encoding' =>'UTF-8','cache_wsdl' => 0));

//内部接口
$ac1='16ucom';
$pw1='c33367701511b4f6020ec61ded352059';
$inSide = new SoapClient(null,array("location" => "http://localhost/open/openService/pft_insideMX.php","uri" => "www.16u.com?ac_16u=$ac1|pw_16u=$pw1|auth_16u=true"));

// $pft_order =  $REQUEST['order']; //获取票付通的订单号
$pft_order =  '433869'; //获取票付通的订单号

//获取订单信息
$r=$inSide->Order_Globle_Search("", "", "", "", "", "", "", "", "", "", "", "",$pft_order, "", "", "", "", "", "", "1", "0",  "1", "0", "0", "", "0","","0","","","0","0",1);
var_dump($r);
$r= simpleXML_load_string($r);

$lid = $r->Rec->UUlid;		//景区id
$UUaid = $r->Rec->UUsellid;  //最顶级的供应商
$UUordername = $r->Rec->UUordername; //姓名
$UUordertel = $r->Rec->UUordertel; //取票人手机号码
$UUtnum = $r->Rec->UUtnum ; //购买数量
$UUbegintime = $r->Rec->UUbegintime;// 游玩时间
$UUdname = $r->Rec->UUdname; //分销商名称
$IDCard = '350321199108230748'; //假设的身份证
$sex = 0 ;//性别(0:男,1女) 假设

//获取携程的编号
// $s =  $outSide->__soapCall("Get_Ticket_List",array("ac"=>$ac,"pw"=>$pw,"in0"=>$lid));
// $s = simpleXML_load_string($s);

$get_xc_ids = (string)$r->Rec->UUgetaddr;
$get_xc_ids = explode("|",$get_xc_ids);
$get_xc_ids = explode("_",$get_xc_ids[1]);
$jd_id = $get_xc_ids[0];  //携程产品编号
$zy_id = $get_xc_ids[1];  //携程资源编号


$initializtion = array('AllianceID'=>22666,'SID'=>456827,'APIKey'=>'899480A9-2747-4F61-A0F2-17302A82B187','reqUri'=>'http://openapi.ctrip.com/vacations/OpenServer.ashx','Uid'=>'69433ba5-d56b-4a44-b1b8-07c9758d52cd','ProtocolType'=>1,'time'=>time());



//获取携程的动态价格
$reqJson=<<<H
{
    "DistributionChannel": 9, 
    "ProductID": {$jd_id}, 
    "ResourcePriceAndInventoryRequestItemList": [
        {
            "EndDate": "{$UUbegintime}T00:00:00", 
            "ResourceID": {$zy_id},
            "StartDate": "{$UUbegintime}T23:59:59"
        }
    ]
}
H;

$initializtion['reqJson'] = str_replace('"','\"',$reqJson);
$initializtion['RequestType'] = 'TicketResourcePriceAndInventory';
$TicketResourcePriceAndInventory = xc_all($initializtion);
$TicketResourcePriceAndInventory = json_decode($TicketResourcePriceAndInventory,true);
$ResponseBody = json_decode($TicketResourcePriceAndInventory['ResponseBody'],true);
$TicketResourcePriceAndInventory = $ResponseBody['ResourcePriceAndInventoryList'];
$xc_price = $TicketResourcePriceAndInventory[0]['ResourcePriceAndInventoryDailyList'];
$Price = $xc_price[0]['Price']; //携程门票资源携程卖价
$Amount = $UUtnum * $Price;
// print_R($Price);exit;
//往携程推送订单

$reqJson=<<<H
{
    "AdultNumber": {$UUtnum},
    "Amount": {$Amount},
    "ClientInfoList": [     
        {
            "AgeType": "ADU",
            "BirthCity": null,
            "BirthDate": "1990-05-01T00:00:00",
            "CardCity": null,
            "ClientName": "{$UUordername}",
            "ClientName_E": "Last",
            "ContactInfo": "{$UUordertel}",
            "Gender": "{$sex}",
            "HzAdd": null, 
            "HzDate": "0001-01-01T00:00:00", 
            "HzNo": "0",
            "IDCardNo": "{$IDCard}",
            "IDCardTimelimit": "0001-01-01T00:00:00",
            "IDCardType": 1,
            "InfoID": 0,
            "IssueDate": "0001-01-01T00:00:00",
            "Nationality": null, 
            "PassportType": "P",
            "VisaCountry": null    
        }
    ], 
    "DistributorOrderID": "{$pft_order}",
    "ContactInfo": {
        "ContactAddress": null, 
        "ContactEmail": "",
        "ContactFax": null, 
        "ContactMobile": "{$UUordertel}",
        "ContactName": "{$UUdname}",
        "ContactTel": null
    }, 
    "ProductInfo": {
        "ProductID": {$jd_id},
    }, 
    "Remark": "无备注说明",
    "SalesCity": 2,
    "TicketInfo": [
        {
            "Price": {$Amount},
            "TicketID": {$zy_id},
            "UseDate": "{$UUbegintime}",
            "Quantity": {$UUtnum},
            "TicketType": 0
        }
    ], 
"Uid": "69433ba5-d56b-4a44-b1b8-07c9758d52cd"
}
H;

// print_r($reqJson);exit;
$initializtion['reqJson'] = str_replace('"','\"',$reqJson);
// print_R($initializtion['reqJson']);exit;
$initializtion['RequestType'] = 'TicketOrderCreateForPrepay_V2';
$TicketOrderCreateForPrepay_V2 = xc_all($initializtion);
print_r($TicketOrderCreateForPrepay_V2);exit;

//以下是函数部分
function xc_all($initializtion){
    $t = $initializtion['time'].$initializtion['AllianceID'].strtoupper(md5($initializtion['APIKey'])).$initializtion['SID'].$initializtion['RequestType'];
	$initializtion['signature'] = strtoupper(md5($t));
	$initializtion = zh_bm($initializtion);//转换编码
	
	$str =<<<h
{"AllianceID":"{$initializtion['AllianceID']}","SID":"{$initializtion['SID']}","ProtocolType":"{$initializtion['ProtocolType']}","Signature":"{$initializtion['signature']}","TimeStamp":"{$initializtion['time']}","Channel":"Vacations","Interface":"{$initializtion['RequestType']}","IsError":false,"RequestBody":"{$initializtion['reqJson']}","ResponseBody":"","ErrorMessage":""}
h;
	$tmp = array(); 
	$tmp['RequestJson'] = urlencode($str);
    $data = "RequestJson=".$tmp['RequestJson'];
   // echo $data;
	$ch = curl_init();//打开
	curl_setopt($ch, CURLOPT_POST, true);
	curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
	curl_setopt($ch, CURLOPT_URL, $initializtion['reqUri']);
	curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
	$response  = curl_exec($ch);
	curl_close($ch);//关闭 
	// $date_response_ResponseBody = json_decode($response,true);
	// $product_date = json_decode($date_response_ResponseBody['ResponseBody'],true);
	return $response;
}

function zh_bm($arr){  //转换编码
 if(is_array($arr)){
   foreach($arr as $k => $v){
     $arr[$k] = zh_bm($v);
   }
   return $arr;
 }
 return iconv("UTF-8","GB2312//IGNORE",$arr);
}
/*
$AllianceID = 22666; //联盟id
$SID = 456827;//站点id
$APIKey = '899480A9-2747-4F61-A0F2-17302A82B187';// 密钥
$Uid = '69433ba5-d56b-4a44-b1b8-07c9758d52cd'; //用户ID
$reqUri = "http://openapi.ctrip.com/vacations/OpenServer.ashx";
$RequestType="TicketOrderCreateForPrepay_V2"; //请求接口服务的名称:门票景点详情
$ProtocolType = 1; //0是xml，1是json
*/
?>